#include <memory>
#include <vector>
#include "UdpServer.hpp"
//#include "Comm.hpp"

using namespace std;

void Usage(string proc)
{
    cout << "Usage: \n\t" << proc << " local_port\n" << endl;
}

string OnMessageDefault(string request)
{
    return request + "[爱你！！！]";
}

vector<string> black_words = {
    "rm",
    "unlink",
    "dd",
    "mv",
    "chmod",
    "exit"
};

bool SafeCheck(string command)
{
    for(auto &k : black_words)
    {
        size_t pos = command.find(k);
        if(pos != string::npos) return false;
    }
    return true;
}

string ExecuteCommand(string command)
{
    if(!SafeCheck(command))
    {
        return "ban man!!!";
    }

    cout << "get a message" << command << endl;
    FILE *fp = popen(command.c_str(), "r");
    if(fp == nullptr)
    {
        return  "popen error, reason is unknow";
    }

    string response;
    char buffer[1024];
    while(true)
    {
        char *s = fgets(buffer, sizeof(buffer), fp);
        if(!s) break;
        else{
            response += buffer;
        }
    }
    return response.empty() ? "success" : response;
}

int main(int argc, char *argv[])
{   
    if(argc != 2)
    {
        Usage(argv[0]);
        return Usage_Err;
    }

    // string ip = argv[1];
    uint16_t port = stoi(argv[1]);
    // unique_ptr<UdpServer> usvr (new UdpServer(OnMessageDefault, port));
    // unique_ptr<UdpServer> usvr (new UdpServer(ExecuteCommand, port));
    // unique_ptr<UdpServer> usvr = make_unique<UdpServer>();

    unique_ptr<UdpServer> usvr (new UdpServer(port));


    usvr->Init();

    usvr->Start();

    return 0;
}